﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Factory.MasterData.Materials.Dto;
using Factory.WarehouseManagement.WarehouseSettings;
using Factory.WarehouseManagement.Vendors.DTO;

namespace Factory.WarehouseManagement.Vendors
{
    public class VendorAppService:ApplicationService, IVendorsAppService
    {
        private readonly IRepository<VendorReturn,int> _vendorAppService;

        public VendorAppService(IRepository<VendorReturn, int> vendorAppService)
        {
            _vendorAppService = vendorAppService;
        }

        public async Task<PageResult> GetListAsync(string returnName, int pageIndex = 1, int pageSize = 10)
        {
            var setlist = await _vendorAppService.GetListAsync(x => x.ReturnName == returnName);

            setlist = setlist.WhereIf(!string.IsNullOrEmpty(returnName), x => x.ReturnName.Contains(returnName)).ToList();
            PageResult pageResult = new PageResult();
            pageResult.RowCount = setlist.Count();
            pageResult.PageCount = (int)Math.Ceiling(pageResult.RowCount * 1.0 / pageSize);
            pageResult.Queryable = setlist.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return pageResult;
        }
        public int CreateAsync(VendorReturn input)
        {
            var warehouseSetlist = _vendorAppService.GetListAsync(x => x.ReturnName == input.ReturnName).Result;

            // 检查是否已存在该单位名称
            if (warehouseSetlist.Count() > 0)
            {
                return -100;  // 表示重复，或者抛出异常等
            }

            var warehouselist = _vendorAppService.InsertAsync(new VendorReturn
            {
                ReturnCode=input.ReturnCode,
                ReturnName = input.ReturnName,
                ProcurementCode=input.ProcurementCode,
                VendorCode=input.VendorCode,
                VendorName = input.VendorName,
                ReturnTime = input.ReturnTime,
                IsDel = input.IsDel

            });
            return 1;
        }
        public async Task<int> UpdateAsync(VendorDTO input)
        {
            var warehouselist = await _vendorAppService.FindAsync(x => x.Id == input.Id);
            if (warehouselist == null)
            {
                // 客户未找到，可以根据需要抛出异常或返回特定值
                return 0;
            }
            warehouselist.ReturnCode = input.ReturnCode;
            warehouselist.ReturnName = input.ReturnName;
            warehouselist.ProcurementCode = input.ProcurementCode;
            warehouselist.VendorCode = input.VendorCode;
            warehouselist.VendorName = input.VendorName;
            warehouselist.ReturnTime = input.ReturnTime;
            warehouselist.IsDel = input.IsDel;


            await _vendorAppService.UpdateAsync(warehouselist);
            return 1; // 或者返回其他表示成功的值
        }

        public async Task<int> DeleteAsync(int id)
        {
            var warehouselist = await _vendorAppService.FindAsync(x => x.Id == id);
            if (warehouselist != null)
            {
                await _vendorAppService.DeleteAsync(warehouselist);
                return 1;
            }
            else
            {
                return 0;
            }
        }

        public VendorReturn GetVendorById(int id)
        {
            var warehouselist = _vendorAppService.FindAsync(id).Result;
            return warehouselist;
        }
    }
}
